
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.Group;
import com.crv.ci.service.IGroupService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class GroupAction extends AbstractAdminController<IGroupService>{

    @Autowired
    private IGroupService groupService;
    
    @RequestMapping(value="/group/list")
    public String list(Group group,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(group); 	
    	BizData4Page<Group> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("group", group);
    	return "/module/group/list";
    }    
    
    @RequestMapping(value="/group/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("group", new Group());
        return "module/group/edit";
    }    
    
    @RequestMapping(value="/group/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	Group group = groupService.fetch(id);
    	model.addAttribute("group", group);
        return "module/group/edit";
    }    
    
    @RequestMapping(value="/group/save")
    @ResponseBody
    public String save(HttpServletRequest request,Group group){
		boolean isAddNew = StringUtils.isBlank(group.getId())?true:false;
		if(isAddNew)
		{
			group.setId(IdGenerator.createNewId());
			groupService.add(group);
		}
		else
		{
			groupService.update(group);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/group/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	groupService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(Group group)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("bizType", group.getBizType());		
		conditions.put("groupType", group.getGroupType());		
		conditions.put("seq", group.getSeq());		
		conditions.put("note", group.getNote());		
		conditions.put("createDate", group.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IGroupService getMainService() {
        return groupService;
    }

    @Override
    protected String getMainObjName() {
        return "group";
    }

    @Override
    protected String getViewTitle() {
        return "group";
    }
}
